package com.example.myapplication;

import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.Snackbar;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;


import com.example.adapter.WorkinfoRecylerView;
import com.example.beans.WorkInfoemation;
import com.example.inter.LoginCallBack;
import com.example.util.WorkTeacher;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class WorkinfoActivity extends AppCompatActivity implements LoginCallBack, View.OnClickListener,SwipeRefreshLayout.OnRefreshListener{
    private RecyclerView recyclerView;
    private WorkinfoRecylerView workinfoRecylerView;
    private List<WorkInfoemation> list = new ArrayList<>();
    private WorkTeacher workTeacher;
    private SwipeRefreshLayout swipeRefreshLayout;
    protected LinearLayoutManager mLayoutManager;
    private StaggeredGridLayoutManager staggeredGridLayoutManager;
    private WorkInfoemation workinfoemation1;

    /***
     * 记录最后一项的位置
     */
    protected int lastVisibleItem=0;
    private int TYPE_NULL= 0;
    private int TYPE_ALL = 1;
    private int TYPE_FRESH = 2;
    private int PAGE = 1;
    private int PAGE_TOTAL=0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_workinfo);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        initView();
        setSupportActionBar(toolbar);
        workinfoemation1 = new WorkInfoemation();
        workinfoemation1.setType(1);
        workTeacher = new WorkTeacher();
        workTeacher.getInfo(PAGE,this);
        workinfoRecylerView = new WorkinfoRecylerView(WorkinfoActivity.this, list);
        mLayoutManager=new LinearLayoutManager(this, LinearLayout.VERTICAL,false);
        recyclerView.setLayoutManager(mLayoutManager);
       /* staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(staggeredGridLayoutManager);*/
        //    recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(workinfoRecylerView);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright, android.R.color.holo_green_light,
                android.R.color.holo_orange_light, android.R.color.holo_red_light);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
        recyclerView.addOnScrollListener(OnSroll);
        recyclerView.setItemAnimator(new DefaultItemAnimator());


    }

    RecyclerView.OnScrollListener OnSroll  = new RecyclerView.OnScrollListener(){
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            onRecyclerViewStateChanged(newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();

        }
    };


    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
    }

    public void onRecyclerViewStateChanged(int newState) {
        if (list == null || list.size() <= 0) {
            Snackbar.make(swipeRefreshLayout, "没有数据得先下拉刷新", Snackbar.LENGTH_SHORT).show();
            return;
        }
        //滚动事件结束并且到达最底端
        if (newState == RecyclerView.SCROLL_STATE_IDLE
                && lastVisibleItem + 1 == workinfoRecylerView.getItemCount()) {
            if (PAGE < PAGE_TOTAL) {
                LoadMore();
            }else{
                Snackbar.make(swipeRefreshLayout, "没有数据了", Snackbar.LENGTH_SHORT).show();
            }
        }
    }

    public void onPreLoadMore() {
        list.add(workinfoemation1);
        workinfoRecylerView.notifyDataSetChanged();
    }

    public void onPostLoadMore() {
        PAGE++;
        workTeacher.getInfo(PAGE,this);


    }

    public void LoadMore(){
        onPreLoadMore();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                onPostLoadMore();
            }
        }, 3000);
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onSuccess(String info) {
        list.remove(workinfoemation1);
        swipeRefreshLayout.setRefreshing(false);
        Log.i("JsonObject", info);
        List<WorkInfoemation> mlist = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(info);
            PAGE_TOTAL = jsonObject.getInt("page");
            JSONArray jsonArray = jsonObject.getJSONArray("info");
            for (int i = 0; i < jsonArray.length(); i++) {
                WorkInfoemation workInfoemation = new WorkInfoemation();
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                String workid = jsonObject1.getString("workid");
                String workname = jsonObject1.getString("workname");
                String workinfo = jsonObject1.getString("workinfo");
                String date = jsonObject1.getString("date");
                String teacher_id = jsonObject1.getString("teacher_id");
                JSONObject jsonObject2 = jsonObject1.getJSONObject("work");
                String teacher_name = jsonObject2.getString("teacher_name");
                String teacher_email = jsonObject2.getString("teacher_email");
                String teacher_number = jsonObject2.getString("teacher_number");
                String teacher_pic = jsonObject2.getString("teacher_pic");
                workInfoemation.setDate(date);
                workInfoemation.setTeacher_name(teacher_name);
                workInfoemation.setTeacher_email(teacher_email);
                workInfoemation.setTeacher_pic(teacher_pic);
                workInfoemation.setTeacher_number(teacher_number);
                workInfoemation.setTeacher_id(teacher_id);
                workInfoemation.setWorkinfo(workinfo);
                workInfoemation.setWorkname(workname);
                workInfoemation.setWorkid(workid);
                workInfoemation.setType(0);
                mlist.add(workInfoemation);

            }

            list.addAll(mlist);
            workinfoRecylerView.changeList(list);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailed() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(true);
        list.clear();
        workinfoRecylerView.changeList(list);
        PAGE = 1;
        workTeacher.getInfo(PAGE,this);
    }


}
